package com.wurd.controller;

import com.wurd.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.UUID;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private DataSource dataSource;

    @RequestMapping("/test")
    public String test() {
        testService.test();
        return "test";
    }

    @RequestMapping("/test1")
    public String test1() {
        try {
            String sql = "select * from test where 1=1 ";
            String str = "性能方面：HikariCP因为细节方面优化力度较大，性能方面强于Druid" +
                    "功能丰富程度方面：Druid功能更全面除了具有连接池的基本功能以外，还支持sql级监控，" +
                    "支持扩展，防止SQL注入等功能。使用热度：Druid在国内使用较多，国内有很多生产实践。" +
                    "HikariCP是spring boot 2.0以后默认连接池，在国外使用较多。";
            Connection conn = dataSource.getConnection();
            for (int i = 0; i < 10000; i++) {
                String u = UUID.randomUUID().toString();
                String finalSql = sql + " or name = '" + str + u + "' or name = ?";
                PreparedStatement prepstm = conn.prepareStatement(finalSql);
                prepstm.setString(1, u);
                Thread.sleep(10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "test";
    }

    @GetMapping("/home")
    public String home() {
        System.out.println();
        return "home";
    }

}

